package com.shaoshaohuo.app.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.OrderShopInfoEntity;
import com.shaoshaohuo.app.utils.im.IMFactory;
import com.squareup.picasso.Picasso;

public class ItemOrderDetails extends AppCompatActivity implements View.OnClickListener {
    /**
     * tobar
     */
    private ImageView order_tobar_back;
    private TextView order_tobar_enter;
    private ImageView order_tobar_right;
    private LinearLayout order_layout_topbar_left;
    private String type;
    private String usertype;
    private String intent_data = "intentData";
    private OrderShopInfoEntity.DataBean dataBean;
    private String status;
    private ImageView order_shop_details_picture;
    private TextView order_shop_details_name;
    private TextView order_shop_details_mbn_details;
    private TextView order_shop_details_peiSong;
    private TextView order_shop_details_lengLian;
    private TextView order_shop_details_details;
    private TextView order_shop_details_money;
    private TextView order_details_all_money;
    private TextView order_details_heji;
    private double total_money;
    private TextView order_details_bianhao;
    private TextView order_details_picture_hint;
    private TextView order_details_picture_hint_word;
    private TextView order_details_push_hour;
    private TextView order_details_push;
    private TextView order_details_pay_moner_hour;
    private TextView order_details_pay_hour;
    private TextView order_details_copy;
    private LinearLayout order_shop_details_hint;
    private RelativeLayout order_details_find_kefu;

    private LinearLayout item_shop_pick_address_linearLayout;
    private TextView item_shop_pick_address;
    private TextView order_shop_num;
    private LinearLayout lin_push;
    private RelativeLayout itemdetails_address;
    private TextView itemdetails__name, itemdetails_phone, item_details_address;

    public ItemOrderDetails newInstance(Intent intent, OrderShopInfoEntity.DataBean dataBean, String status, String type, String usertype) {
        ItemOrderDetails orderDetails = new ItemOrderDetails();
        Bundle bundle = new Bundle();
        bundle.putSerializable(intent_data, dataBean);
        bundle.putString("status", status);
        bundle.putString("type", type);
        bundle.putString("usertype", usertype);
        intent.putExtras(bundle);
        return orderDetails;
    }

    public void getIntenData() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            dataBean = (OrderShopInfoEntity.DataBean) extras.getSerializable(intent_data);
            status = extras.getString("status");
            type = extras.getString("type");
            usertype = extras.getString("usertype");//1商户采摘2用户采摘
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_details);
        getIntenData();
        initView();
        setView();
        decideType();
        initListener();
    }

    private void initListener() {
        order_tobar_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        order_details_copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager copy_word = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                String copy_trim = order_details_bianhao.getText().toString().trim();
                ClipData clipData = ClipData.newPlainText("Label", copy_trim);
                copy_word.setPrimaryClip(clipData);
                if (copy_word.getPrimaryClip().toString().trim() != null) {
                    Toast.makeText(getApplicationContext(), "复制成功", Toast.LENGTH_SHORT).show();
                }
            }
        });

        order_details_find_kefu.setOnClickListener(this);
    }

    private void decideType() {
        if (type.equals("1")) {
            itemdetails_address.setVisibility(View.VISIBLE);
            item_details_address.setText(dataBean.getReAddress());
            itemdetails__name.setText(dataBean.getReName());
            itemdetails_phone.setText(dataBean.getRePhone());
            if (status.equals("1")) {
                order_details_picture_hint_word.setText("购物愉快");
                order_details_picture_hint.setText("请尽快支付");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
                lin_push.setVisibility(View.GONE);
//                order_details_pay_moner_hour.setText(dataBean.getOrder_time());
                order_details_pay_hour.setText("");
            } else if (status.equals("2")) {
                lin_push.setVisibility(View.GONE);
                order_details_picture_hint_word.setText("请耐心等待");
                order_details_picture_hint.setText("我们会尽快为您发货");
                order_details_pay_hour.setText(dataBean.getPay_time());
                order_details_push_hour.setVisibility(View.VISIBLE);
                order_details_push.setVisibility(View.GONE);
                order_details_pay_moner_hour.setText(dataBean.getOrder_time());
                order_details_pay_hour.setText("付款时间:");
//                order_details_push.setText("付款时间:");
                order_details_push_hour.setText(dataBean.getPay_time());
            } else if (status.equals("3")) {
                lin_push.setVisibility(View.VISIBLE);
                order_details_picture_hint_word.setText("请耐心等待");
                order_details_picture_hint.setText("卖家已发货");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
//                order_details_push.setText("发货时间:");
//                order_details_push_hour.setText(dataBean.getPush_time());
                order_details_pay_moner_hour.setText(dataBean.getPay_time());
                order_details_pay_hour.setText("付款时间:");
            } else if (status.equals("4")) {
                lin_push.setVisibility(View.VISIBLE);
                order_details_picture_hint_word.setText("请耐心等待");
                order_details_picture_hint.setText("正在退款");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
//                order_details_push.setText("付款时间:");
//                order_details_push_hour.setText(dataBean.getOrder_time());
                order_details_pay_moner_hour.setText(dataBean.getPay_time());
                order_details_pay_hour.setText("付款时间:");
            } else if (status.equals("5")) {
                order_details_picture_hint_word.setText("购物愉快");
                order_details_picture_hint.setText("交易完成");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
//                order_details_push.setText("发货时间:");
                lin_push.setVisibility(View.VISIBLE);
//                order_details_push_hour.setText(dataBean.getPush_time());
                order_details_pay_moner_hour.setText(dataBean.getPay_time());
                order_details_pay_hour.setText("付款时间:");
            } else if (status.equals("6")) {
                order_details_picture_hint_word.setText("购物愉快");
                order_details_picture_hint.setText("退款成功");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
//                order_details_push.setText("付款时间:");
                lin_push.setVisibility(View.VISIBLE);
//                order_details_push_hour.setText(dataBean.getOrder_time());
                order_details_pay_moner_hour.setText(dataBean.getPay_time());
                order_details_pay_hour.setText("付款时间:");
            } else if (status.equals("7")) {
                order_details_picture_hint_word.setText("购物愉快");
                order_details_picture_hint.setText("交易完成");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
//                order_details_push.setText("发货时间:");
                lin_push.setVisibility(View.VISIBLE);
//                order_details_push_hour.setText(dataBean.getPush_time());
                order_details_pay_moner_hour.setText(dataBean.getPay_time());
                order_details_pay_hour.setText("付款时间:");
            } else if (status.equals("8")) {
                lin_push.setVisibility(View.VISIBLE);
                order_details_picture_hint_word.setText("请耐心等待");
                order_details_picture_hint.setText("正在退款");
                order_details_push_hour.setVisibility(View.GONE);
                order_details_push.setVisibility(View.GONE);
//                order_details_push.setText("付款时间:");
//                order_details_push_hour.setText(dataBean.getOrder_time());
                order_details_pay_moner_hour.setText(dataBean.getPay_time());
                order_details_pay_hour.setText("付款时间:");
            }
        }
    }

    private void setView() {
        if (type.equals("1")) {
            item_shop_pick_address_linearLayout.setVisibility(View.GONE);
            order_shop_details_hint.setVisibility(View.VISIBLE);
            order_shop_details_details.setVisibility(View.VISIBLE);
            order_shop_details_details.setText("(一件起售 " + dataBean.getDetails() + ")");
            order_shop_num.setText("×" + dataBean.getBuy_num());
            order_shop_details_mbn_details.setText(dataBean.getMbn_details());
        } else if (type.equals("2")) {
            lin_push.setVisibility(View.GONE);
            order_shop_details_mbn_details.setVisibility(View.GONE);
            order_shop_details_hint.setVisibility(View.GONE);
            order_shop_details_details.setVisibility(View.GONE);
            order_shop_num.setVisibility(View.GONE);
            order_details_picture_hint_word.setVisibility(View.GONE);
            order_details_picture_hint.setVisibility(View.GONE);
            order_details_push_hour.setVisibility(View.GONE);
            order_details_pay_moner_hour.setText(dataBean.getPay_time());
            if (usertype.equals("2")) {
                item_shop_pick_address_linearLayout.setVisibility(View.VISIBLE);
                item_shop_pick_address.setText(dataBean.getAddress());
            }
            if (usertype.equals("1")) {
                item_shop_pick_address_linearLayout.setVisibility(View.GONE);
            }
        }
        order_shop_details_name.setText(dataBean.getName());
        order_details_bianhao.setText(dataBean.getOrderid());
        order_shop_details_money.setText("￥" + Double.valueOf(dataBean.getFin_money()) / (Double.valueOf(dataBean.getBuy_num())));
        if (dataBean.getImg() != null) {
            Picasso.with(this).load(dataBean.getImg()).into(order_shop_details_picture);
        }
//        if (dataBean.getBuy_num() != null && dataBean.getFin_money() != null) {
//            Double number = Double.valueOf(dataBean.getBuy_num());
//            Double money = Double.valueOf(dataBean.getFin_money());
//            total_money = number * money;
//        }
        order_details_heji.setText("￥" + dataBean.getFin_money());
        order_details_all_money.setText("￥" + dataBean.getFin_money());
    }

    private void initView() {
        /**
         * tobar
         * */

        order_tobar_back = (ImageView) findViewById(R.id.imageview_topbar_left_backImage);
        order_tobar_back.setVisibility(View.VISIBLE);
        order_tobar_back.setImageResource(R.mipmap.return_key);
        order_tobar_enter = (TextView) findViewById(R.id.toolbar_topname);
        order_tobar_enter.setText("订单详情");
        order_tobar_right = (ImageView) findViewById(R.id.imageview_message);
        order_tobar_right.setVisibility(View.GONE);
        order_layout_topbar_left = (LinearLayout) findViewById(R.id.layout_topbar_left);
        order_layout_topbar_left.setVisibility(View.VISIBLE);

        order_shop_details_picture = (ImageView) findViewById(R.id.order_shop_details_picture);
        order_shop_details_name = (TextView) findViewById(R.id.order_shop_details_name);
        order_shop_details_mbn_details = (TextView) findViewById(R.id.order_shop_details_mbn_details);
        order_shop_details_peiSong = (TextView) findViewById(R.id.order_shop_details_peiSong);
        order_shop_details_lengLian = (TextView) findViewById(R.id.order_shop_details_lengLian);
        order_shop_details_details = (TextView) findViewById(R.id.order_shop_details_details);
        order_shop_details_money = (TextView) findViewById(R.id.order_shop_details_money);
        order_details_all_money = (TextView) findViewById(R.id.order_details_all_money);
        order_details_heji = (TextView) findViewById(R.id.order_details_heji);
        order_details_bianhao = (TextView) findViewById(R.id.order_details_bianhao);
        order_details_picture_hint = (TextView) findViewById(R.id.order_details_picture_hint);
        order_details_picture_hint_word = (TextView) findViewById(R.id.order_details_picture_hint_word);
        order_details_pay_moner_hour = (TextView) findViewById(R.id.order_details_pay_moner_hour);
        order_details_pay_hour = (TextView) findViewById(R.id.order_details_pay_hour);
        order_details_push_hour = (TextView) findViewById(R.id.order_details_push_hour);
        order_details_push = (TextView) findViewById(R.id.order_details_push);
        order_details_copy = (TextView) findViewById(R.id.order_details_copy);
        order_details_find_kefu = (RelativeLayout) findViewById(R.id.order_details_find_kefu);

        lin_push = (LinearLayout) findViewById(R.id.lin_push);
        order_shop_details_hint = (LinearLayout) findViewById(R.id.order_shop_details_hint);
        item_shop_pick_address_linearLayout = (LinearLayout) findViewById(R.id.item_shop_pick_address_linearLayout);
        item_shop_pick_address = (TextView) findViewById(R.id.item_shop_pick_address);
        order_shop_num = (TextView) findViewById(R.id.order_shop_num);

        itemdetails_address = (RelativeLayout) findViewById(R.id.itemdetails_address);
        itemdetails__name = (TextView) findViewById(R.id.itemdetails__name);
        itemdetails_phone = (TextView) findViewById(R.id.itemdetails_phone);
        item_details_address = (TextView) findViewById(R.id.item_details_address);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.order_details_find_kefu:
                IMFactory.openChatServiceContact(this);
                break;
        }
    }
}
